package com.zx.zxapiclientsdk.config;

import com.zx.zxapiclientsdk.client.ZxAPIClient;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

import java.lang.reflect.Constructor;

/**
 * @author 张旭
 * @version 1.0
 */
@Data
@Configuration
@ConfigurationProperties("zxapi.client")
@ComponentScan
public class ZxApiClientConfig {

    private Integer appId;
    private String accessKey;
    private String secretKey;

    @Bean
    public ZxAPIClient apiClient() {
        ZxAPIClient client = null;
        try {
            Class<?> forName = Class.forName("com.zx.zxapiclientsdk.client.ZxAPIClient");
            Constructor<?> declaredConstructor = forName.getDeclaredConstructor(Integer.class, String.class, String.class);
            declaredConstructor.setAccessible(true);//将访问安全检查的开关禁用，提高效率
            client = (ZxAPIClient) declaredConstructor.newInstance(appId, accessKey, secretKey);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return client;
    }
}
